Sistemas Lineares

  • Eliminação de Gauss
  • Método de Gauss-Jordan
  • Método de Jacobi

Eliminação de Gauss


In [10]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%autosave 120


Autosaving every 120 seconds

In [12]:
def eliminacao_de_gauss(A, b):
    G = np.append(A, b, 1).astype(float)
    n = len(G)
    
    for i in range(x-1):
        if G[i, i] == 0:
            return np.zeros((n, n + 1))
        for j in range(i + 1, n):
            G[j] -= G[i] * G[j, i] / G[i, i]
    
    return G
    
#     for i in range(n):
#         for j in range(i+1, n):
#             G[j] -= G[i] * G[j,i] / G[i, i]

# A = np.array([[3, 3, -1], [2, 6, 2], [8, 4, 1]])
# b = np.array([[6, 20, 19]]).T

# G = np.append(A, b, 1).astype(float)
# G

Gauss Jordan


In [14]:
def gauss_jordan(A, b):
    G = np.append(A, b, 1).astype(float)
    n = len(G[:, 0])
    for i in range(n - 1):
        if G[i, i] == 0:
            return np.zeros((n, n - 1))
        for j in range(i + 1, n):
            G[j] -= G[i] * G[j, i] / G[i, i]
    
    for i in range(n - 1, -1, -1):
        G[i] /= G[i, i]
        for j in range(i-1, -1, -1):
            G[j] -= G[i] * G[j, i]
    
    return G[:, n:]

Método de Jacobi


In [15]:
def jacobi(A, b, x):
    x_novo = x
    n = len(x)
    
    for i in range(n):
        x_novo[i] = b[i]
        for j in range(n):
            if i == j:
                continue
            x_novo[i] -= x[j] * A[i, j] / A[i, i]
            
    x = x_novo
    return x

In [ ]: